package com.example.smartcitydemo.viewModel.SubwayViewModel

import androidx.lifecycle.*
import com.example.smartcity_jscs.network.ResultStates.Resource
import com.example.smartcitydemo.bean.*
import com.example.smartcitydemo.ext.getLiveData
import com.example.smartcitydemo.repo.subway.SubwayRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn

/**
 * @PACKAGE_NAME: com.example.smartcitydemo.viewModel.SubwayViewModel
 * @NAME: Caleb.F
 * @DATE: 2021/11/15 8:57
 * @Email: 1919796283@qq.com
 */
class SubWayViewModel : ViewModel() {


    private val mutableBanner = MutableLiveData<String>()
    private val mutableLocation = MutableLiveData<String>()
    private val mutableWeatherInfo = MutableLiveData<String>()
    private val mutableLineDetail = MutableLiveData<String>()
    private val mutableStationInfo = MutableLiveData<String>()


    val homeBanner = mutableBanner.switchMap {
        getLiveData { SubwayRepo.getSubWayHomeBanner() }
    }


    val weatherInfo: LiveData<Resource<WeatherInfo>> = mutableWeatherInfo.switchMap {
        getLiveData { SubwayRepo.getTodayWeatherInfo() }
    }


    val locationInfo: LiveData<Resource<String>> = mutableLocation.switchMap {
        getLiveData { SubwayRepo.getLoactionInfo() }
    }

    val lineInfo = mutableLineDetail.switchMap {
        getLiveData { SubwayRepo.getLineData() }
    }

    //配置唯一可信源
    val stationInfo: LiveData<Resource<List<StationDataBean>>> = mutableStationInfo.switchMap {
        if (it == "all") {
            getLiveData { SubwayRepo.getStationFull() }
        } else {
            getLiveData { SubwayRepo.getStationList(it) }
        }
    }


    fun getWeatherInfo() {
        mutableWeatherInfo.value = mutableWeatherInfo.value
    }


    fun getLocationInfo() {
        mutableLocation.value = mutableLocation.value
    }

    fun getHomeBanner() {
        mutableBanner.value = mutableBanner.value
    }

    fun getLineInfo() {
        mutableLineDetail.value = mutableLineDetail.value
    }

    fun getStationFull() {
        mutableStationInfo.value = "all"
    }

    fun getStationInfo(lineId: Int) {
        mutableStationInfo.value = "$lineId"
    }


    private val mutableImageString = MutableLiveData<String>()

    val subWayImageUrl: LiveData<Resource<SubWayImage>> = mutableImageString.switchMap {
        getLiveData { SubwayRepo.getSubWayImage() }
    }

    fun getSubWayImage() {
        mutableImageString.value = mutableImageString.value
    }

    /**
     * 获取乘客须知
     */
    private val mutableSubWayBroadString = MutableLiveData<String>()

    val subwayBroadData: LiveData<Resource<SubWayPassengerNotice>> =
        mutableSubWayBroadString.switchMap {
            getLiveData { SubwayRepo.getSubWayBroadCastFirst() }
        }

    fun getSubWayBroad() {
        mutableSubWayBroadString.value = mutableSubWayBroadString.value
    }

    /**
     *倒计时按钮的实现
     *
     */
    val broadButtonLoadTime =
        flow {
            for (i in 5 downTo 0 step 1) {
                emit("${i}s")
                kotlinx.coroutines.delay(1000)
            }
        }.flowOn(Dispatchers.IO).asLiveData()

    /**
     * 获取地铁通知
     */
    private val mutableSubWayNewsString = MutableLiveData<String>()
    val subwayNoticeData: LiveData<Resource<List<SubWayNoticeBean>>> =
        mutableSubWayNewsString.switchMap {
            getLiveData { SubwayRepo.getSubWayNotice() }
        }
    fun getSubWayNotice() {
        mutableSubWayNewsString.value = mutableSubWayNewsString.value
    }


    /**
     * 获取首页站点
     */
    private val mutableHomeStationData = MutableLiveData<String>()

    fun getHomeStationInfo() {
        mutableHomeStationData.value = "建国门"
    }

    val homeStationInfo: LiveData<Resource<List<CurrentStationDetail>>> =
        mutableHomeStationData.switchMap {
            getLiveData { SubwayRepo.getSubWayHomeDataDetail(it) } }

}